import SnowBall2 from "./SnowBall2";

export default class SnowMan2 extends Laya.Script {
    
    /** @prop {name:snowBallPrefab,tips:"雪球",type:Prefab}*/
    snowBallPrefab: Laya.Prefab;


    constructor() { super(); }

    ownerRig: Laya.RigidBody;

    isMove = false;
    angle = 0;
    realOwner: Laya.Sprite;

    speed = 5;

    isRobot = false;
    snowBallCollider: Laya.CircleCollider;
    isRebound = false;

    onEnable(): void {
        this.realOwner = this.owner as Laya.Sprite;

        this.ownerRig = this.realOwner.getComponent(Laya.RigidBody) as Laya.RigidBody;
    }

    onDisable(): void {

    }

    addSnowBall() {
        let snowBallCollider = new Laya.CircleCollider();
        snowBallCollider.radius = 25;
        snowBallCollider.x = 150;
        snowBallCollider.y = 25;
        this.realOwner.addComponentIntance(snowBallCollider)
        this.snowBallCollider = snowBallCollider;

    }

    startGrownUp() {
        Laya.timer.frameLoop(5, this, this.grownUp);
    }

    stopGrownUp() {
        Laya.timer.clear(this, this.grownUp);
    }
    grownUp() {
        if (this.snowBallCollider) {
            this.snowBallCollider.radius++;
            this.snowBallCollider.y = (100 - this.snowBallCollider.radius * 2) / 2;
        }
    }

    onUpdate() {
        if (this.isRebound) {
            if (this.ownerRig.linearVelocity.x < 1 && this.ownerRig.linearVelocity.y < 1) {
                this.isRebound = false;
            }
        } else {
            if (this.isMove) {
                let v = this.getVectorByAngle(this.angle, this.speed);
                this.ownerRig.setVelocity(v);
                this.realOwner.rotation = -(this.angle - 2 * Math.PI) / Math.PI * 180;
            } else {
                this.ownerRig.setVelocity({ x: 0, y: 0 });
            }
        }


        // ai move
    }

    startMove() {
        this.isMove = true;
        this.startGrownUp();
    }

    stopMove() {
        this.isMove = false;
        this.stopGrownUp();
        this.emitBall();
    }

    changeDirection(angle: number) {
        this.angle = angle;
    }

    emitBall() {

        let x = this.realOwner.x
        let y = this.realOwner.y;
        let radius = this.snowBallCollider.radius;
        let distance = 100 + radius;
        let v = this.getVectorByAngle(this.angle, distance)
        let point = {
            x: x+v.x,
            y: y+v.y
        };

        

        this.snowBallCollider.enabled = false;
        this.snowBallCollider.destroy();
        this.snowBallCollider = null;
        this.realOwner._destroyComponent(this.snowBallCollider)


        let ball = this.snowBallPrefab.create() as Laya.Sprite;

        // this.realOwner.snowMan.addChild(ball);
        ball.width = radius * 2;
        ball.height = radius * 2;
        ball.pivot(radius /2, radius /2);
        ball.pos(point.x, point.y)
        this.realOwner.parent.addChild(ball);
        let ballRig = ball.getComponent(Laya.RigidBody) as Laya.RigidBody;
        let script = ball.getComponent(SnowBall2) as SnowBall2;
        script.setRadius(radius);
        let ballV = this.getVectorByAngle(this.angle, this.speed*2);
        

        ballRig.setVelocity(ballV);
    }

    onTriggerEnter(other: any, self: any, contact: any) {
        let snowMan = self.owner as Laya.Sprite;
        let snowBall = other.owner as Laya.Sprite;


        let angle = Math.atan2(snowMan.y - snowBall.y, snowMan.x - snowBall.x);
        angle = Math.PI * 2 - angle;

        this.isRebound = true;

        let v = this.getVectorByAngle(angle, 20);

        let rig = self.rigidBody as Laya.RigidBody;
        rig.setVelocity(v);
    }


    getVectorByAngle(angle: number, speed: number) {
        let x = Math.cos(angle) * speed;
        let y = -Math.sin(angle) * speed;
        return {
            x, y
        }
    }
}